const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        name: '',
        mobile: '',
        prov: '',
        city: '',
        dist: '',
        address: ''
    },
    setName: function(e){
        this.setData({
            name: e.detail.value
        })
    },
    setMobile: function(e){
        this.setData({
            mobile: e.detail.value
        })
    },
    setAddress: function(e){
        this.setData({
            address: e.detail.value
        })
    },
    bindRegionChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            prov: e.detail.value[0],
            city: e.detail.value[1],
            dist: e.detail.value[2],
        })
    },

    save: function(){
        var name = this.data.name, mobile = this.data.mobile, address = this.data.address, prov = this.data.prov, city = this.data.city, dist = this.data.dist;
        if(!name || !mobile || !address || !prov){
            wx.showToast({
              title: '请填写完整信息',
              icon: 'none'
            });
            return false;
        }
        app.ajax({
            url: '/api/user/setAddress',
            data: {
                name: name,
                mobile: mobile,
                address: address,
                prov: prov,
                city: city,
                dist: dist
            },
            success: res => {
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
                if(res.code == 1){
                    setTimeout(function(){
                        wx.navigateBack({
                          delta: 1,
                        })
                    }, 1500);
                }
            }
        }, 1);
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getAddress();
    },

    getAddress: function(){
        app.ajax({
            url: '/api/user/getAddress',
            success: res => {
                if(res.code == 1){
                    this.setData({
                        name: res.data.name,
                        prov: res.data.prov,
                        city: res.data.city,
                        dist: res.data.dist,
                        mobile: res.data.mobile,
                        address: res.data.address
                    })
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})